<template>
  <ModalFormTemplate
    ref="modalRef"
    :open="open"
    :formConfig="formConfig"
    :title="title"
    :width="width"
    @cancel="onCancel"
    @finish="onFinish"
  />
</template>
<script setup lang="ts">
  import { computed, ref } from 'vue';
  import ModalFormTemplate from '@/components/template/form/modal-form-template.vue';

  const title = '商户资料添加'; //弹窗标题
  const width = 550; //弹窗宽度
  const modalRef = ref(); //通用模态弹窗组件引用，用与调用模态弹窗组件内部方法
  const open = ref(false); //控制弹窗显示、隐藏
  const emits = defineEmits<{ dataReload: [] }>(); //通知调用组件(父组件) 执行 父组件 的 @dataReload 的方法

  defineExpose({ show }); //暴露接口 show(显示弹窗) 方法，供 父组件 调用

  /* [关闭弹窗] 1. 重置表单 2. 关闭弹窗 */
  function onCancel() {
    modalRef.value.resetForm();
    open.value = false;
  }

  /* [显示弹窗] 1. 显示弹窗  */
  function show() {
    open.value = true;
  }

  /* [提交弹窗] 1. 调用子组件 提交方法 2. 提交成功后，关闭弹窗 3. 刷新父组件数据 */
  async function onFinish() {
    const params = modalRef.value?.getFormParams();
    const mergedParams = { ...addParams, ...params }; //合并提交参数
    try {
      const ret = await modalRef.value?.postFormData(addAction, mergedParams);
      if (ret === true) {
        onCancel();
        emits('dataReload');
      }
    } catch (error) {}
  }
  const addAction = 'base.merchant.mchchannel.ServiceImpl.save'; //save接口 方法名
  const addParams = { mch_channel_id: 0 }; //save接口 参数

  /* [表单配置] */
  const formConfig = computed(() => [
    {
      title: '所属商户ID',
      dataIndex: 'mch_id',
      type: 'select' as const,
      required: true,
      props: {
        options: {
          action: 'base.merchant.mch.ServiceImpl.listSelectByMchId',
          params: {},
        },
      },
    },
    {
      title: '支付配置ID',
      dataIndex: 'mch_pay_config_id',
      type: 'select' as const,
      required: true,
      props: {
        options: {
          action: 'base.merchant.mchpayconfig.ServiceImpl.listSelectByMchConfigId',
          params: {},
        },
      },
    },
    {
      title: '通道编码',
      dataIndex: 'channel_code',
      type: 'input' as const,
      maxlength: 50,
      required: true,
      defaultValue: '',
    },
    {
      title: '通道名称',
      dataIndex: 'channel_name',
      type: 'input' as const,
      maxlength: 50,
      required: true,
      defaultValue: '',
    },
    {
      title: '支付方式编码',
      dataIndex: 'pay_way_code',
      type: 'input' as const,
      required: true,
      maxlength: 50,
      defaultValue: '',
    },
    {
      title: '支付方式名称',
      dataIndex: 'pay_way_name',
      type: 'input' as const,
      required: true,
      defaultValue: '',
      maxlength: 50,
    },
    {
      title: '支付方式费率',
      dataIndex: 'pay_way_rate',
      type: 'input-number' as const,
      required: true,
      defaultValue: '',
    },
    {
      title: '风控数据',
      dataIndex: 'risk_data',
      type: 'input' as const,
      defaultValue: '',
    },
    {
      title: '路由排序',
      dataIndex: 'sort',
      type: 'input-number' as const,
      defaultValue: '',
      required: true,
    },
    {
      title: '状态',
      dataIndex: 'mch_channel_status',
      type: 'input-number' as const,
      required: true,
      defaultValue: '',
    },
    {
      title: '备注',
      dataIndex: 'remark',
      type: 'input' as const,
      defaultValue: '',
      maxlength: 250,
    },
  ]);
</script>
<style lang="less" scoped></style>
